﻿using BCCommon;
using BCCommon.Enums;
using BCCommon.PaymentEnums;
using BCDto.TMS.AgentFlowRecord;
using BCDto.TMS.AgentWallet;
using BCService.SmsService;
using BCService.TMS.AgentWallet;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BCTMSRESTful.Controllers.Management.TMS.AgentWallet
{
    /// <summary>
    /// 代理商钱包
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    public class AgentWalletController : ManagementBaseController
    {
        private readonly IAgentWalletService agentWalletService;
        private readonly IAliyunSmsService aliyunSmsService;
        public AgentWalletController(IAgentWalletService agentWalletService,
            IAliyunSmsService aliyunSmsService)
        {
            this.agentWalletService = agentWalletService;
            this.aliyunSmsService = aliyunSmsService;
        }

        /// <summary>
        /// 获取钱包信息
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(AgentWalletDto), 200)]
        public IActionResult Get()
        {
            if (Admin.AgentRecord==null)
            {
                throw new ArgumentException(" 您不是代理商,无权查看 ");
            }
            return Ok(this.agentWalletService.GetWalletByAgentId(this.Admin.AgentRecord.AgentRecordId));
        }

        #region 首次设置支付密码
        /// <summary>
        /// 首次设置支付密码
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(AgentWalletDto), 200)]
        public IActionResult FirstSetPayPassword([FromBody] AgentWalletRequestDto requestDto)
        {
            return Ok(this.agentWalletService.FirstSetPayPassword(this.Admin, requestDto));
        }
        #endregion

        #region 重置设置支付密码
        /// <summary>
        /// 重置设置支付密码
        /// </summary>
        /// <response code="200">返回钱包信息</response>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(AgentWalletDto), 200)]
        public IActionResult ResetPayPassword([FromBody] AgentWalletResetRequestDto walletRequestDto)
        {
            if (walletRequestDto.OperatingType == ResetOperatingType.Reset)
            {
                if (!this.aliyunSmsService.ValidatePassCode(this.Admin.Phone, walletRequestDto.PassCode))
                {
                    throw new ArgumentException("验证码验证失败");
                }
            }
            if (walletRequestDto.OperatingType == ResetOperatingType.Modify)
            {
                if (string.IsNullOrWhiteSpace(walletRequestDto.SourcePassword))
                {
                    throw new ArgumentException("原始支付密码不能为空");
                }
            }
            if (string.IsNullOrWhiteSpace(walletRequestDto.PayPassword))
            {
                throw new ArgumentException("支付密码不能为空");
            }

            return Ok(this.agentWalletService.ResetPayPassword(this.Admin.Phone, this.Admin.AgentRecord.AgentRecordId, walletRequestDto));
        }
        #endregion

        /// <summary>
        /// 查看指定代理商的账单明细
        /// </summary>
        /// <param name="flowType">流水类型</param>
        /// <param name="flowDirectionType">流水方向</param>
        /// <param name="minAmount">最小金额</param>
        /// <param name="maxAmount">最大金额</param>
        /// <param name="paymentChannelType">支付渠道</param>
        /// <param name="ascending">排序</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">指定用户的账单明细列表</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<AgentFlowRecordDto>), 200)]
        public IActionResult AgentFlowRecord(
            [FromQuery(Name = "flow_type")] FlowType? flowType,
            [FromQuery(Name = "flow_direction_type")] FlowDirectionType? flowDirectionType,
            [FromQuery(Name = "min_amount")] decimal? minAmount,
            [FromQuery(Name = "max_amount")] decimal? maxAmount,
            [FromQuery(Name = "payment_channel_type")] PaymentChannelType? paymentChannelType,
            [FromQuery(Name = "start_date")] DateTime? startDate,
            [FromQuery(Name = "end_date")] DateTime? endDate,
            [FromQuery(Name = "ascending")] bool? ascending = null,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            if (Admin.AgentRecord == null)
            {
                throw new ArgumentException(" 您不是代理商,无权查看 ");
            }
            return this.Ok(this.agentWalletService.AgentFlowRecord(Admin.AgentRecord.AgentRecordId, flowType, flowDirectionType, minAmount, maxAmount,
                paymentChannelType, startDate, endDate, ascending, pageIndex, pageSize));
        }

    }
}
